﻿using System.Web;
using Summer.Core.Ioc;


#region License
/*
 *  Copyright (c) Dumuzi.cn 2009 , All rights reserved.
 *  CLR Version: 2.0.50727.4927
 *  NameSpace: Summer.Web.Support
 *  Author:Edwin Tai,戴佳顺
 *  Msn:edwin19861218@hotmail.com
 *  QQ: 1961218
 *  Date:2009/10/30 14:42:23
 *  Licensed under the Microsoft Public License (Ms-PL)
 *  This license governs use of the accompanying software. 
 *  If you use the software, you accept this license.
 *  If you do not accept the license, do not use the software.
 */
#endregion

namespace Summer.Web.Support
{
    /// <summary>
    /// Base container implementation in the framework
    /// All the container is defined in it
    /// </summary>
    public class SummerHttpApplication : AbstractExtensibleHttpApplication<HttpApplication>
    {
        private IocConfiguration m_configuration;
        protected HttpApplicationState m_httpapplication;
        private string m_contextxml;
        /// <summary>
        /// The default path of config file
        /// </summary>
        private const string DEFAULT_PATH = @"context.xml";

        public SummerHttpApplication()
            : base()
        {
            this.m_contextxml = DEFAULT_PATH;
        }

        public SummerHttpApplication(string path)
            : base()
        {
            this.m_contextxml = path;
        }

        /// <summary>
        /// Application start event to load all the framework containers and initialize them
        /// </summary>
        /// <param name="additional">HttpApplication object to get current's applicationState</param>
        /// <returns></returns>
        protected override bool ApplicationStart(HttpApplication additional)
        {
            logger.Info("Begin to load Context");
            //Get current applicationState object
            this.m_httpapplication = additional.Application;
            //Load container into applicationState which is under web
            this.m_httpapplication.LoadContainer(this.m_contextxml, Configuration);
            logger.Info("End to load Context");
            return true;
        }
        /// <summary>
        /// Application end event to unload all the framework container
        /// </summary>
        /// <returns></returns>
        protected override bool ApplicationEnd()
        {
            this.m_httpapplication.UnloadContainer();
            return base.ApplicationEnd();
        }
        /// <summary>
        /// Load configuration 
        /// </summary>
        public IocConfiguration Configuration
        {
            set { this.m_configuration = value; }
            get
            {
                if (this.m_configuration == null)
                {
                    return new IocConfiguration();
                }
                return m_configuration;
            }

        }
    }
}
